import Vue from "vue";
import VueRouter from "vue-router";
import login from "../views/login.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "login",
    component: login,
  },
  {
    path: "/register",
    name: "register",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/register.vue"),
  },
  {
    path: "/forgetPassword",
    name: "forgetPassword",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/forgetPassword.vue"),
  },
  {
    path: "/studentHome",
    name: "studentHome",
    meta: { roles: ["0"] }, //自定义属性，角色判断，0为学生
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/student/studentHome.vue"),
    //路由嵌套
    children: [
      {
        //题库练习
        path: "exam",
        name: "exam",
        meta: { roles: ["0"] },
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/student/exam.vue"),
      },
      {
        //考试须知
        path: "homepage",
        name: "homepage",
        meta: { roles: ["0"] },
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/student/homepage.vue"),
      },
      {
        //修改密码
        path: "changePassword",
        name: "SchangePassword",
        meta: { roles: ["0"] },
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/changePassword.vue"),
      },
      {
        //更换头像
        path: "changeAvatar",
        name: "SchangeAvatar",
        meta: { roles: ["0"] },
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/changeAvatar.vue"),
      },
      {
        //下载
        path: "download",
        name: "download",
        meta: { roles: ["0"] },
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/student/download.vue"),
      },
      {
        //考试信息
        path: "examInfo",
        name: "examInfo",
        meta: { roles: ["0"] },
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/student/examInfo.vue"),
      },
      {
        //我的考试 路由传参 需要在myExam.vue使用props接收
        path: "myExam/:eid",
        name: "myExam",
        meta: { roles: ["0"] },
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/student/myExam.vue"),
        props: true,
      },
      {
        //考试详情
        path: "examDetails",
        name: "examDetails",
        meta: { roles: ["0"] },
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/student/examDetails.vue"),
        props: true,
      },
      {
        //查看考试详情
        path: "lookExamDetails/:eid",
        name: "lookExamDetails",
        meta: { roles: ["0"] },
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/student/lookExamDetails.vue"
          ),
        props: true,
      },
      {
        //个人信息
        path: "myInfomation",
        name: "myInfomation",
        meta: { roles: ["0"] },
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/student/myInfomation.vue"),
      },
    ],
  },
  //教师端
  {
    path: "/teacherHome",
    name: "teacherHome",
    meta: { roles: ["1"] },
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/teacher/teacherHome.vue"),
    children: [
      {
        //首页
        path: "teacherHomepage",
        name: "teacherHomepage",
        meta: { roles: ["1"] },
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/teacher/teacherHomepage.vue"),
      },
      {
        //更换头像
        path: "changeAvatar",
        name: "TchangeAvatar",
        meta: { roles: ["1"] },
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/changeAvatar.vue"),
      },
      {
        //修改密码
        path: "changePassword",
        name: "TchangePassword",
        meta: { roles: ["1"] },
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/changePassword.vue"),
      },
      {
        //出卷
        path: "setExam",
        name: "setExam",
        meta: { roles: ["1"] },
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/teacher/setExam.vue"),
      },
      {
        //查询试卷
        path: "searchExam",
        name: "searchExam",
        meta: { roles: ["1"] },
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/teacher/searchExam.vue"),
      },
      {
        //查看试卷
        path: "looksearchExam/:eid",
        name: "looksearchExam",
        meta: { roles: ["1"] },
        props:true,
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/teacher/looksearchExam.vue"),
      },
      {
        //发布考试
        path: "releaseExam",
        name: "releaseExam",
        meta: { roles: ["1"] },
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/teacher/releaseExam.vue"),
      },
      {
        //阅卷
        path: "markingExamPapers",
        name: "markingExamPapers",
        meta: { roles: ["1"] },
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/teacher/markingExamPapers.vue"),
      },
      {
        //阅卷
        path: "markingExamPapersDetails/:eid&:major&:classroom",
        name: "markingExamPapersDetails",
        meta: { roles: ["1"] },
        props:true,
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/teacher/markingExamPapersDetails.vue"),
      },
      {
        //查询发布的考试信息
        path: "searchExaminfo",
        name: "searchExaminfo",
        meta: { roles: ["1"] },
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/teacher/searchExaminfo.vue"),
      },
      {
        //班级管理
        path: "classManagement",
        name: "classManagement",
        meta: { roles: ["1"] },
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/teacher/classManagement.vue"
          ),
      },
      {
        //学生信息
        path: "studentInfo",
        name: "studentInfo",
        meta: { roles: ["1"] },
        component: () =>
          import( /* webpackChunkName: "about" */ "../views/teacher/studentInfo.vue"),
      },
      {
        //学生考试信息
        path: "studentExamInfo",
        name: "studentExamInfo",
        meta: { roles: ["1"] },
        component: () =>
          import( /* webpackChunkName: "about" */ "../views/teacher/studentExamInfo.vue"),
      },
      {
        //个人信息
        path: "teacherInfomation",
        name: "teacherInfomation",
        meta: { roles: ["1"] },
        component: () =>
          import( /* webpackChunkName: "about" */ "../views/teacher/teacherInfomation.vue"),
      },
    ],
  },
  {
    path: "*",
    name: "404",
    meta: { roles: ["0", "1"] },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../components/404.vue"),
  },
  {
    path: "/error",
    name: "error",
    meta: { roles: ["0", "1"] },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../components/error.vue"),
  },
];

const router = new VueRouter({
  routes,
});

//路由守卫
router.beforeEach((to, from, next) => {
  //获取用户信息
  let isLogin = localStorage.getItem("userName");
  let role = localStorage.getItem("userType");
  if (
    //如果要去往的地址为
    to.path == "/" ||
    to.path == "/register" ||
    to.path == "/forgetPassword"
  ) {
    //那就继续执行
    next();
  } else if (isLogin) {
    //做判断，如果用户信息存在
    for (let i = 0; i < to.meta.roles.length; i++) {
      if (role === to.meta.roles[i]) {
        //要去往的页面rules是否与当前role相等
        next(); //角色匹配已登录，正常进入to.meta.roles[i]的页面
        break; //跳出循环
      } else {
        next("/error");
      }
    }
  } else {
    //如果用户信息不存在则跳转到login页面
    next("/");
  }
});

export default router;
